<?php

class AdminController extends Controller
{
    public function init()
    {
        parent::init();
        $this->R->module = "管理员管理";
    }
	public function actionIndex()
	{
		$this->render('index');
	}

    public function actionList() 
    {
        $roleid = (int)$this->getParam('roleid');
        $page = $this->getParam('page');
        $rows = $this->getParam('rows');
        $criteria = new CDbCriteria();
        $criteria->order = '`ID` desc';
        if ($roleid)
        {
            $criteria->addCondition("`RoleID` = $roleid");
        }
        $arr = array();
        $count = Admin::model()->count($criteria);
        $criteria->limit = $rows;
        $criteria->offset = ($page - 1) * $rows;
        $arr['total'] = $count;
        $admins = Admin::model()->findAll($criteria);
        foreach ($admins as $admin)
        {
            $arr['rows'][] = $admin->attributes;
        }
        
        if (!$count)
        {
            $arr['rows'] = array();
        }
        
        echo json_encode($arr);exit;
    }
    //创建或编辑管理员用户
    public function actionCreate() 
    {

        $id = $this->getParam('ID');
        if ($id)
        {
            $this->R->action = '修改管理员用户';
            $model = Admin::model()->findByPk($id);
        }
        else
        {
            $this->R->action = '添加管理员用户';
            $model = new Admin;
        }
        $model->Password = md5($this->getTrimParam('Password'));
        $model->Name = $this->getTrimParam('Name');
        $model->RoleID = (int)$this->getTrimParam('RoleID');
        $model->Desc = $this->getTrimParam('Desc');
        $this->R->success = $model->save()? true : false;
        $this->R->flush();
    }
    //删除用户
    public function actionDelete(){
        $id = $this->getParam('id');
        $this->R->action = '删除用户';
        $this->R->noalert = true;
        if (Admin::model()->deleteByPk($id))
        {
            $this->R->success = true;      
            $this->R->msg = '操作成功';
        }
        else
        {
            $this->R->success = false;
            $this->R->msg = '操作失败';
        }
        $this->R->flush();
    }
    //修改用户密码
    public function actionChangePwd()
    {
        $this->R->action = '修改密码';
        $admin = Admin::model()->find("`ID` =" . Yii::app()->user->id ." AND `Password`='".md5($this->getParam('password'))."'");
        if ($admin !== null)
        {
            $admin->Password = md5($this->getParam('newPassword'));
            if ($admin->validate() && $admin->save())
            {
                $this->R->success = true;
            }
            else
            {
                $this->R->success = false;
                $this->R->msg = '操作失败';
            }
        }
        else
        {
            $this->R->success = false;
            $this->R->msg = '原密码不正确';
        }
        $this->R->flush();
    }

    //检查用户名是否唯一
    public function actionCheckUnique()
    {
        $name = $this->getTrimParam('Name');
        $id = $this->getTrimParam('id');
        $model = Admin::model()->find('Name = :name',array(':name'=>$name));
        if ($id)
        {
            $model = Admin::model()->find('Name = :name AND ID !=:id',array(':name'=>$name,':id'=>$id));
        }
        if ($model == null)
        {
            echo 'true';exit;
        }
        echo 'false';exit;
    }
}